import React, { useContext } from "react";
import { useNavigate } from "react-router-dom";
import { context } from "../../components/AppProvider";
import { Button, Form, Input } from "antd";

import "./login.less";

export default function Login() {
  // 跳转
  const navigate = useNavigate();
  const { resetMenus } = useContext(context);
  const onFinish = (yesFinish) => {
    setTimeout(() => {
      sessionStorage.setItem("token", "token");
      // 刷新页面导致路由以及丢失menu的关键
      sessionStorage.setItem("role", "admin");
      //重置路由菜关键点
      resetMenus("admin");
      // 获取查询参数,如果没有就跳转到首页
      navigate("/layouts/home", { replace: true });
    }, 1000);
  };

  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div className="login">
      <div className="login-form">
        <Form
          name="basic"
          labelCol={{
            span: 8,
          }}
          wrapperCol={{
            span: 16,
          }}
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="账号"
            name="username"
            rules={[
              {
                required: true,
                message: "请输入账号!",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="password"
            rules={[
              {
                required: true,
                message: "请输入登录密码!",
              },
            ]}
          >
            <Input.Password />
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit">
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}
